<template>
  <div class="comment-container">
    <el-card  style="margin: 0 20px 20px 0;">

      <el-row style="margin: 10px">
        <el-col :span="24">
          <!-- <el-button type="primary" size="small" icon="el-icon-plus"
          @click="addClick"
            >添加</el-button
          > -->
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="deleteClick"
          >批量删除</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table
        :data="commentList"
        style="width: 100%"
        stripe
        lazy
        @selection-change="handleSelectionChange"
      >>
        <el-table-column type="selection" width="50" />
        <el-table-column prop="comment_user.user_name" label="昵称" width="80" align="center">
          <!-- <template v-slot="slot">
            <el-avatar :size="24" :src="slot.row.comment_user.avatar"></el-avatar>
            {{slot.row.comment_user.user_name}}
          </template> -->
        </el-table-column>
        <el-table-column label="类型" width="57" align="center">
          <template v-slot="slot">
            <el-tag v-if="slot.row.parentId===0" type="success">评论</el-tag>
            <el-tag v-else>回复</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reply_user.user_name" width="80" align="center" />
        <el-table-column prop="content" label="内容" />
        <el-table-column prop="comment_article.title" label="所属文章" />
        <el-table-column
          prop="createdAt"
          label="时间"
          width="160"
          align="center"
        />
        <el-table-column prop="stutas" label="状态" width="80" align="center">
          <template v-slot="slot">
            <!-- 1:审核中;2:审核通过;3审核失败;) -->
            <el-tag v-if="slot.row.status==1" type="info">审核中</el-tag>
            <el-tag v-else-if="slot.row.status==2" type="success">审核通过</el-tag>
            <el-tag v-else type="danger">审核失败</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="address" label="操作" width="180" align="center">
          <template v-slot="slot">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              @click="auditClick(slot.row)"
            >审核</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="deleteClick(slot.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 审核对话框 -->
    <el-dialog
      title="审核评论"
      :visible.sync="auditDialogVisible"
      width="30%"
      center
      :before-close="handleClose"
      @close="close"
    >
      <el-radio-group v-model="radio">
        <el-radio :label="1">审核中</el-radio>
        <el-radio :label="2">审核通过</el-radio>
        <el-radio :label="3">审核失败</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="auditOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCommentList, deleteComment, getCommenId, editCommen } from '@/api/comment'
export default {
  name: 'Comment',
  data() {
    return {
      commentList: [], // 文章表格数据
      queryInfo: {
        query: '', // 查询参数
        pageNum: 1, // 分页页码
        pageSize: 10 // 页面数据条数
      },
      total: 0,
      selectRows: [], // 全选数据
      auditDialogVisible: false,
      radio: 1,
      rows: {}
    }
  },
  created() {
    this.getCommentList()
  },
  methods: {
    async auditOk() {
      const id = this.rows.id
      const body = this.radio
      await editCommen(id, { status: body })
      this.auditDialogVisible = !this.auditDialogVisible
      this.getCommentList()
      const res = await getCommenId(id)
      switch (res.status) {
        case 1:
          this.$message.info('审核状态为：审核中')
          break
        case 2:
          this.$message.success('审核状态为：审核通过')
          break
	  case 3:
          this.$message.warning('审核状态为：审核失败')
          break
      }
    },
    close() {
      // this.radio = 1
    },
    handleClose(done) {
      done()
    },
    // 数据
    async getCommentList() {
       this.queryInfo.id=this.$route.query.id
      
      const { list, pageNum, pageSize, total } = await getCommentList(
        this.queryInfo
      )
      this.commentList = list
      this.queryInfo.pageNum = Number(pageNum)
      this.queryInfo.pageSize = Number(pageSize)
      this.total = total
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.getCommentList()
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum = val
      this.getCommentList()
    },
    // 点击审核
    async auditClick(row) {
      const res = await getCommenId(row.id)
      this.radio = res.status
      this.rows = res
      this.auditDialogVisible = !this.auditDialogVisible
    },
    // 删除
    deleteClick(row) {
      if (row.id) {
        this.$confirm('你确定要删除选中项吗', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            await deleteComment({ ids: [row.id] })
            this.getCommentList()
            this.$message.success('删除成功')
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      } else {
        // 批量删除
        if (!this.selectRows.length) { return this.$message.warning('请选择要删除的选中项') }
        const ids = this.selectRows.map((item) => item.id)
        this.$confirm('你确定要删除选中项吗', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            await deleteComment({ ids })
            this.getCommentList()
            this.$message.success('删除成功')
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      }
    },
    // 全选数据
    handleSelectionChange(val) {
      this.selectRows = val
    }
  }
}
</script>

<style lang="scss"  scoped>
.el-select .el-input {
  width: 80px;
}
// .box-card {
//   margin: 20px;
// }
.el-table {
  margin: 20px 0px;
}
.el-image {
  border-radius: 5px;
}
</style>
